###################################################################################

library(data.table)
library(optparse)
library(dplyr)

###################################################################################

option_list <- list(
    make_option(c("--input_file"), type = "character") ,
    make_option(c("--tcga_file"), type = "character") ,
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    input_file <- "/public/home/xxf2019/20220915_gastric_multiple/dna_combinePublic/public_ref/combine/MutationInfo.combine.addMolecularSubType.tsv"
    tcga_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/TCGA/stad_tcga_pan_can_atlas_2018_clinical_data.tsv"
    out_path <-"/public/home/xxf2019/20220915_gastric_multiple/dna_combinePublic/public_ref/images/mutBurden"

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

input_file <- opt$input_file
tcga_file <- opt$tcga_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

##############################################################################

dat <- data.frame(fread(input_file))
tcga_info <- data.frame(fread(tcga_file))
tcga_info <- tcga_info[,c("Patient.ID" , "Race.Category")]

##############################################################################
## 标记人种
dat <- merge( dat , tcga_info , by.x = "Tumor" , by.y = "Patient.ID" , all.x = T )
dat$Race.Category <- ifelse( dat$From %in% c("Utokyo" , "NJMU" , "TMUCIH" , "OncoSG") , "Asian" , dat$Race.Category )
dat$Race.Category[is.na(dat$Race.Category)] <- "unknown"


out_name <- paste0(out_path , "/MutationInfo.combine.addMolecularSubType.Race.tsv")
write.table( dat, out_name , row.names = F , quote = F , sep = "\t" )